package test.core;

import com.thoughtworks.selenium.*;

public class DefaultSeleniumEx extends DefaultSelenium {
	
	public DefaultSeleniumEx(java.lang.String serverHost, int serverPort, java.lang.String browserStartCommand, java.lang.String browserURL){
		super(serverHost, serverPort, browserStartCommand, browserURL);
	}
	
	  /**
     * Waiting for element to appear/disappear on page for specified time (means not element visibility but really
     * present/not present like in {@link com.thoughtworks.selenium.Selenium#isElementPresent(String)} method).
     *
     * @param locator an element locator.
     * @param timeout time to wait in milliseconds after which {@link TimeoutException} will be thrown.
     * @param present true - wait until element appear, false - wait until element disappear.
     * @since SeleniumRC 1.0.1
     */
    public void waitForElement(String locator, String timeout, boolean present) {
        String logMessage = "Waiting for element '" + locator + "' to " + (present ? "" : "dis") + "appear for " + timeout + " milliseconds... Element ";
        try {
            waitForCondition("var element;" +
                    "try{" +
                    " element = selenium.browserbot.findElement('" + locator.replaceAll("'", "\\\\'") + "');" +
                    "} catch(e){" +
                    " element = null;" +
                    "}" +
                    "element" + (present ? "!=" : "==") + "null;", timeout);
            logMessage += (present ? "" : "dis") + "appear.";
        } catch (SeleniumException e) {
            logMessage += "didn't " + (present ? "" : "dis") + "appear as expected!";
            throw new RuntimeException(logMessage, e);
        }
    }
    
    /**
     * Waiting for element to appear on page for specified time. It's the same as {@link #waitForElement(String, String,
     * boolean)} when the third parameter is true.
     *
     * @param locator an element locator.
     * @param timeout time to wait in milliseconds after which {@link TimeoutException} will be thrown.
     * @since SeleniumRC 1.0.1.
     */
    public void waitForElementAppear(String locator, String timeout) {
        waitForElement(locator, timeout, true);
    }

    /**
     * Waiting for element to disappear on page for specified time. It's the same as {@link #waitForElement(String,
     * String, boolean)} when the third parameter is false.
     *
     * @param locator an element locator.
     * @param timeout time to wait in milliseconds after which {@link TimeoutException} will be thrown.
     * @since SeleniumRC 1.0.1.
     */
    public void waitForElementDisappear(String locator, String timeout) {
        waitForElement(locator, timeout, false);
    }
    
    /**
     * Waiting for element to visible on page for specified time.
     *
     * @param locator an element locator.
     * @param timeout time to wait in milliseconds after which {@link TimeoutException} will be thrown.
     */
    public void waitForElementVisible(String locator, String timeout) {
    	waitForCondition("selenium.isVisible(\"" + locator + "\")", timeout);
    }

    /**
     * Waiting for element to not visible on page for specified time.
     *
     * @param locator an element locator.
     * @param timeout time to wait in milliseconds after which {@link TimeoutException} will be thrown.
     */
    public void waitForElementNotVisible(String locator, String timeout) {
    	waitForCondition("!selenium.isVisible(\"" + locator + "\")", timeout);
    }
    
}
